package cn.edu.cug.cs.gtl.apis.ui.projecttree;


import cn.edu.cug.cs.gtl.api.wrapper.ProjectWrapper;
import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.projecttree.*;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeEditor;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeRenderer;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Map;
import cn.edu.cug.cs.gtl.protos.Project;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import java.awt.*;

public class ProjectPanel extends JPanel {

    AbstractApplication theApp = null;
    ProjectTree treeComponent = null;
    JScrollPane scrollPane = null;

    public ProjectPanel(AbstractApplication theApp,LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        this.theApp=theApp;
    }

    public ProjectPanel(AbstractApplication theApp,LayoutManager layout) {
        super(layout);
        this.theApp=theApp;
    }

    public ProjectPanel(AbstractApplication theApp,boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        this.theApp=theApp;
    }

    public ProjectPanel(AbstractApplication theApp) {
        setLayout(new BorderLayout());
        this.theApp=theApp;
    }

    public ProjectTree getTreeComponent() {
        return treeComponent;
    }

    public void initial(){
        if(theApp.getProject()==null) {
            theApp.setProject(ProjectWrapper.createDefaultProject());
        }

        {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode(new TreeNodeData(theApp.getProject(),theApp.getProject().getName(),true));

            for(int i=0;i<this.theApp.getProject().getMapCount();++i){
                Map map = this.theApp.getProject().getMap(i);
                DefaultMutableTreeNode mapNode = new DefaultMutableTreeNode(new TreeNodeData(map,map.getName(),true));
                for(int j=0;j<map.getLayerCount();++j){
                    Layer layer = map.getLayer(j);
                    mapNode.add(new DefaultMutableTreeNode(new TreeNodeData(layer,layer.getName(),true)));
                }
                root.add(mapNode);
            }
            TreeModel treeModel = new DefaultTreeModel(root);
            treeComponent = new ProjectTree(treeModel);

            final TreeNodeRenderer renderer = new TreeNodeRenderer();
            treeComponent.setCellRenderer(renderer);

            final TreeNodeEditor editor = new TreeNodeEditor(treeComponent);
            treeComponent.setCellEditor(editor);
            treeComponent.setEditable(true);


            treeComponent.addTreeSelectionListener(new ProjectTreeSelectionListener(treeComponent,this.theApp));
            treeModel.addTreeModelListener(new ProjectTreeModelListener(treeComponent,this.theApp));
            treeComponent.addMouseListener(new ProjectTreeMouseListener(treeComponent,this.theApp));

            scrollPane = new JScrollPane(treeComponent);
            add(scrollPane, BorderLayout.CENTER);
        }
    }

    public void reset(Project project){

        if(treeComponent!=null) {
            treeComponent.removeAll();

        }

        if(theApp.getProject()==null) {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Project");
            treeComponent.setModel(new DefaultTreeModel(root));
        }
        else {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode(new TreeNodeData(theApp.getProject(),theApp.getProject().getName(),true));

            for(int i=0;i<this.theApp.getProject().getMapCount();++i){
                Map map = this.theApp.getProject().getMap(i);
                DefaultMutableTreeNode mapNode = new DefaultMutableTreeNode(new TreeNodeData(map,map.getName(),true));
                for(int j=0;j<map.getLayerCount();++j){
                    Layer layer = map.getLayer(j);
                    mapNode.add(new DefaultMutableTreeNode(new TreeNodeData(layer,layer.getName(),true)));
                }

                root.add(mapNode);
            }
            DefaultTreeModel treeModel = new DefaultTreeModel(root);
            treeModel.addTreeModelListener(new ProjectTreeModelListener(treeComponent,this.theApp));
            treeComponent.updateUI();
            treeComponent.setModel(treeModel);
            treeComponent.setAutoscrolls(true);
            treeComponent.setScrollsOnExpand(true);
            treeComponent.invalidate();
            treeComponent.repaint();
            theApp.getMainFrame().invalidate();
        }
    }
}
